/*
 * Sound.cpp
 *
 *  Created on: 07/03/2013
 *      Author: herbert
 */

#include "component/Sound.h"
#include <iostream>

using namespace eats_core;
using namespace std;

Sound_::Sound_(string _song_location,bool isMusic ){
	if(isMusic){
		playLikeAMusic = true;
	} else {
		playLikeAMusic = false;
	}
	soundLocation = _song_location;
}

Sound_::~Sound_() {

}

bool Sound_::setUp(){
	bool ret = true;
	Load_Sound();
}

bool Sound_::tearDown() {
	cout << "|INFO| Fechando o canal de som para esse componente... " << endl;
	Close_Sound();
}

bool Sound_::Load_Sound()
{
	if(playLikeAMusic){
		music = Mix_LoadMUS(soundLocation.c_str());
	} else {
		effect = Mix_LoadWAV(soundLocation.c_str());
		if(effect == NULL){
			cout << "ERRO DESCONHECIDO" << endl;
			return false;
		}
	}
	return true;
}

void Sound_::Play_Sound()
{
	if(playLikeAMusic) {
		Mix_PlayMusic( music, -1);
	} else {
		Mix_PlayChannel(-1, effect, 0);
	}
}

void Sound_::Close_Sound()
{
	if(playLikeAMusic) {
		if(music != NULL)
			Mix_FreeMusic(music);
	} else {
		if(effect != NULL)
		Mix_FreeChunk( effect );
	}
}


